package com.songcha.sakura.mvvm.local.video

import android.app.Application
import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.mvvm.base.BaseRepository
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.sakura.struct.M3u8LocalFile
import com.songcha.sakura.struct.VideoType
import com.songcha.sakura.util.FFMpegUtil
import com.songcha.sakura.util.FileUtil
import com.songcha.library_common.util.ToastUtil
import java.io.File


class LocalVideoViewModel(app: Application) : BaseViewModel<LocalVideoRepository>(app) {

     var fileList:MutableLiveData<MutableList<M3u8LocalFile>> = MutableLiveData()
     var isConverting=MutableLiveData<Boolean>()
     var isCopying=MutableLiveData<Boolean>()
     //var isGetingLocalFile=MutableLiveData<Boolean>()

     //val m3u8List = MutableLiveData<MutableList<String>>()
    init {
         fileList.value=mutableListOf()
    }

    fun ts2Mp4(video:M3u8LocalFile.LocalVideo){
        if(video.type!=VideoType.M3U8) return
        if(video.tsCount==0) return

        Thread{
            isConverting.postValue(true)
            val ts_path=video.absoluteEpisodePath
            val mp4_path=video.comicPath+"/"+(video.episodePath.substring(0,video.episodePath.length-"m3u8_0".length))+"mp4"
            val ret= FFMpegUtil.ts2Mp4(ts_path,mp4_path,video.tsCount)
            if(ret==0){
                FileUtil.deleteDirWithFile(File(video.absoluteEpisodePath))
                val m3u8=video.comicPath+"/"+video.episodePath.substring(0,video.episodePath.length-"_0".length)
                val m3u8_index=m3u8+".index"
                FileUtil.delete(m3u8)
                FileUtil.delete(m3u8_index)
                ToastUtil.show("转换成功")
            }else{
                ToastUtil.show("转换失败")
            }
            isConverting.postValue(false)
        }.start()

    }

   /* fun copy2Public(video:M3u8LocalFile.LocalVideo){
        if(video.type!=VideoType.MP4) return
        Thread{
            isCopying.postValue(true)
            val sandBoxVideoPath= DownloadConfig.getVideoDownloadPath()
            val path=sandBoxVideoPath+"/"+video.episodePath.substring(1)
            //File(sandBoxVideoPath+"/.333.txt").createNewFile()
            val ret=FileUtil.copyfile(File(video.absoluteEpisodePath),File(path))
            if(ret){
                ToastUtil.show("复制成功")
            }else{
                ToastUtil.show("复制失败")
            }
            isCopying.postValue(false)
        }.start()

    }*/

    fun deleteVideo(video:M3u8LocalFile.LocalVideo){
        //val comicPath=context.filesDir.absolutePath+"/"+video.title+"/"
        val comicFile= File(video.comicPath)
        //删除视频文件，如果只有一个，删除番剧文件夹
        if(video.type!=VideoType.M3U8){
            FileUtil.delete(video.absoluteEpisodePath)
        }else{
            FileUtil.deleteDirWithFile(File(video.absoluteEpisodePath))
            val m3u8=video.comicPath+"/"+video.episodePath.substring(0,video.episodePath.length-"_0".length)
            val m3u8_index=m3u8+".index"
            FileUtil.delete(m3u8)
            FileUtil.delete(m3u8_index)
        }

        val files=comicFile.listFiles()
        var count=0
        if(files!=null)  {
            for(file in files){
                if(file.isDirectory || file.name.endsWith(".mp4")){
                    count++
                    break
                }
            }
        }
        if(count==0){
            FileUtil.delete(video.comicPath+"/thumb.jpg")
            FileUtil.deleteDirWithFile(comicFile)
        }

    }

     fun getLocalVideoList(context: Context){
         Thread{
            // isGetingLocalFile.postValue(true)
             val list=fileList.value ?: return@Thread
             list.clear()
             val tmp=(repository!! as LocalVideoRepository).getLocalVideoList()
             list.addAll(tmp)
             fileList.postValue(list)
             //isGetingLocalFile.postValue(false)
         }.start()


    }

     /*fun addM3u8(url:String){
          if(m3u8List.value==null){
               m3u8List.value=mutableListOf()
          }
          m3u8List.value?.add(url)
          val a=0
     }

     fun clearM3u8List(){
          m3u8List.value?.clear()
     }

     fun getM3u8List():MutableList<String>?{
          return m3u8List.value
     }*/

}